package com.snowRemovingUser.controller;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.snowRemovingUser.domain.TCsvAbnormal;
import com.snowRemovingUser.service.ITCsvAbnormalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 异常处理Controller
 *
 * @author lishiguang
 * @date 2023-05-08
 */
@RestController
@RequestMapping("/snowRemoving/abnormal")
public class TCsvAbnormalController extends BaseController
{
    @Autowired
    private ITCsvAbnormalService tCsvAbnormalService;

    /**
     * 查询异常处理列表
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:abnormal:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCsvAbnormal tCsvAbnormal)
    {
        startPage();
        List<TCsvAbnormal> list = tCsvAbnormalService.selectTCsvAbnormalList(tCsvAbnormal);
        return getDataTable(list);
    }

    /**
     * 导出异常处理列表
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:abnormal:export')")
    @Log(title = "异常处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCsvAbnormal tCsvAbnormal)
    {
        List<TCsvAbnormal> list = tCsvAbnormalService.selectTCsvAbnormalList(tCsvAbnormal);
        ExcelUtil<TCsvAbnormal> util = new ExcelUtil<TCsvAbnormal>(TCsvAbnormal.class);
        util.exportExcel(response, list, "异常处理数据");
    }

    /**
     * 获取异常处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:abnormal:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tCsvAbnormalService.selectTCsvAbnormalById(id));
    }

    /**
     * 新增异常处理
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:abnormal:add')")
    @Log(title = "异常处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCsvAbnormal tCsvAbnormal)
    {
        return toAjax(tCsvAbnormalService.insertTCsvAbnormal(tCsvAbnormal));
    }

    /**
     * 修改异常处理
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:abnormal:edit')")
    @Log(title = "异常处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCsvAbnormal tCsvAbnormal)
    {
        return toAjax(tCsvAbnormalService.updateTCsvAbnormal(tCsvAbnormal));
    }

    /**
     * 删除异常处理
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:abnormal:remove')")
    @Log(title = "异常处理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tCsvAbnormalService.deleteTCsvAbnormalByIds(ids));
    }


    /**
     * 一键处理异常
     * @param id
     * @return
     */
    @GetMapping(value = "/handleAbnormal/{id}")
    public AjaxResult handleAbnormal(@PathVariable("id") Long id)
    {
        return toAjax(tCsvAbnormalService.handleAbnormal(id));
    }


    /**
     *  HsoIndex 文件上传
     * @param file
     * @param id
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) throws IOException, SftpException, JSchException {

        return toAjax(tCsvAbnormalService.uploadByHsoIndex(file,id));
    }
    /**
     * 自行处理异常
     * @param id
     * @return
     */
    @GetMapping(value = "/handleDiscretion/{id}")
    public AjaxResult handleDiscretion(@PathVariable("id") Long id)
    {
        return toAjax(tCsvAbnormalService.handleDiscretion(id));
    }
}
